/**
 * Copyright 2009 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.sapien.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.validation.SkipValidation;

import com.google.code.sapien.model.User;
import com.google.code.sapien.service.InstallService;
import com.google.inject.Inject;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.EmailValidator;
import com.opensymphony.xwork2.validator.annotations.FieldExpressionValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator;

/**
 * Install Action, performs operations that take the database from an empty state to a default
 * seeded state.
 * @author Adam
 * @version $Id$
 * 
 * Created on May 28, 2009 at 8:44:40 PM 
 */
public class InstallAction extends ActionSupport {
    /**
	 * Serial Version UID.
	 */
	private static final long serialVersionUID = 4963917746170867561L;

	/**
	 * The install service.
	 */
	private final InstallService installService;

	/**
     * Properties used to create the administrative user account.
     */
    private String username, email, password, password2;

	/**
	 * Constructs an install action.
	 * @param installService The install service.
	 */
	@Inject
	public InstallAction(final InstallService installService) {
		this.installService = installService;
	}

	/**
	 * {@inheritDoc}
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override @SkipValidation
	public String execute() {
		return SUCCESS;
	}
	/**
	 * Returns email.
	 * @return the email.
	 */
	@RequiredStringValidator(message = "Required Field.", key = "validation.required")
    @EmailValidator(message = "Enter a valid email.", key = "validation.email")
	public String getEmail() {
		return email;
	}
	/**
	 * Returns password.
	 * @return the password.
	 */
	@RequiredStringValidator(message = "Required Field.", key = "validation.required")
    @StringLengthFieldValidator(trim = true, minLength = "3" , maxLength = "16",
    		message = "Must be between ${minLength} and ${maxLength} characters.", key = "validation.length")
	public String getPassword() {
		return password;
	}

	/**
	 * Returns password2.
	 * @return the password2.
	 */
	@RequiredStringValidator(message = "Required Field.", key = "validation.required")
	@FieldExpressionValidator(expression = "password2.equals(password)",
            message = "Passwords do not match.", key = "validation.match.passwords")
	public String getPassword2() {
		return password2;
	}

	/**
	 * Returns username.
	 * @return the username.
	 */
	@RequiredStringValidator(message = "Required Field.", key = "validation.required")
    @StringLengthFieldValidator(trim = true, minLength = "3" , maxLength = "16",
    		message = "Must be between ${minLength} and ${maxLength} characters.", key = "validation.length")
	public String getUsername() {
		return username;
	}

	/**
	 * Starts the installation procedure.
	 * @return SUCCESS.
	 */
	@Action(value = "start-install", results = {
			@Result(name = "input", location = "install.jsp")
	})
	public String install() {
		User admin = new User();
		admin.setUsername(username);
		admin.setPassword(password);
		admin.setEmail(email);
		installService.install(admin);
		return SUCCESS;
	}

	/**
	 * Sets email.
	 * @param email the email to set.
	 */
	public void setEmail(final String email) {
		this.email = email;
	}

	/**
	 * Sets password.
	 * @param password the password to set.
	 */
	public void setPassword(final String password) {
		this.password = password;
	}

	/**
	 * Sets password2.
	 * @param password2 the password2 to set.
	 */
	public void setPassword2(final String password2) {
		this.password2 = password2;
	}

	/**
	 * Sets username.
	 * @param username the username to set.
	 */
	public void setUsername(final String username) {
		this.username = username;
	}
}
